<?php

namespace app\admin\controller;

use app\admin\model\ActivityClass;
use think\Controller;
use think\Request;

class ActivityClassController extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $classes = ActivityClass::paginate(10);
        return view('index', compact('classes'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        //
        //
        if ($request->isPost()) {
            $data = $request->post();
            $validate = $this->validate($data, [
                'name' => 'require',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            $class = new ActivityClass();
            $res = $class->insert($data);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        }
        return view('add');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request, $id)
    {
        //
        $class = ActivityClass::get($id);
        if ($request->isPost()) {
            $data = $request->post();
            $validate = $this->validate($data, [
                'name' => 'require',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            $res = $class->allowField(true)->save($data);
            //halt($ids);
            if ($res) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败', 'edit');
            }
        }
        return view('edit', compact('class'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function del($id)
    {
        //
        $category = ActivityClass::get($id);
        $res = $category->delete();
        if ($res) {
            return $this->success('删除成功', 'index');
        } else {
            return $this->error('删除失败', 'index');
        }
    }
}
